package com.polaris.live.ui.main.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.lxj.xpopup.XPopup
import com.polaris.live.cache.CommonCache
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.resp.AppVersionVo
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.ActivityCeshiBinding
import com.polaris.live.dialog.AwardProgressDialog
import com.polaris.live.dialog.ChargeFailureDialog
import com.polaris.live.dialog.GlobalDialogManager
import com.polaris.live.utils.LiveUtils
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.StartLiveProxy
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.viewmodel.TestViewModel
import com.polaris.live.widget.marqueen.MarqueeFactory
import com.polaris.live.widget.marqueen.SimpleNoticeMF

/**
 * 测试页
 *
 * @author Created by 长空 on 2023/10/21/009 15:47
 */
class CeShiActivity : BaseVbActivity<TestViewModel, ActivityCeshiBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        val datas: List<String> = mutableListOf(
            "《ikun》",
            "I'm not lazy, I'm just on energy-saving mode",
            "People advise me, but when have I ever listened to them?",
            "I used to be indecisive. Now I'm not so sure.",
            "I'm reading a book about anti-gravity. It's impossible to put down!"
        )

        val marqueeFactory = SimpleNoticeMF(this) as MarqueeFactory<TextView, String>
        mBinding.marqueeView1.setMarqueeFactory(marqueeFactory)
        mBinding.marqueeView1.startFlipping()
        marqueeFactory.setData(datas)
        marqueeFactory.setOnItemClickListener(object : MarqueeFactory.OnItemClickListener<TextView, String> {
            override fun onItemClick(view: View?, holder: MarqueeFactory.ViewHolder<TextView, String>?) {

            }

        })
        mBinding.my.setOnSingleClickListener {
            Router.instance.setPath(RoutePath.SET_ACTIVITY).navigation(this)
        }
        mBinding.my.text = "${UserManager.getUser().userId}"

        mBinding.goToParty.setOnSingleClickListener {
            Router.instance.setPath(RoutePath.SET_PARTY_COVER_ACTIVITY).navigation(this)
        }
        mBinding.sevenDay.setOnSingleClickListener {
            GlobalDialogManager.showWeekPackPrice(mViewModel)
        }

        mBinding.gameCenter.setOnSingleClickListener {
            mViewModel.addFollower(3010)
        }
        mBinding.sixin.setOnSingleClickListener {
            val id = mBinding.otherId.text.toString()

            if (!TextUtils.isEmpty(id)) {
                Router.builder(RoutePath.MESSAGE_ACTIVITY)
                    .withInt("page", UserConst.From.UNKNOWN)
                    .withLong("userId", id.toLong())
                    .navigation(this)
            }
        }
        mBinding.cover.setOnSingleClickListener {
            Router.instance.setPath(RoutePath.SET_LIVE_IMAGE_ACTIVITY).navigation(this)
        }
        mBinding.textRouter.setOnSingleClickListener {
//            val router =
//                "native://className=KJRichCharmViewController&androidUrl=/live/LiveSpectatorActivity?anchorId=3010&router=native%3A%2F%2Faction%3Dgift"
//            RouterUtils.routerGoto(routerPath)
//            RouterUtils.routerGoto("native://androidUrl=/party/PartySpectatorActivity?anchorId=3271&router=native%3A%2F%2Faction%3Dgift")
//            RouterUtils.routerGoto("native://androidUrl=/set/SetPartyCoverActivity?gameId=4&rewardType=0&rewardNum=10000")
            val router = "native://action=firstLive"
//            RouterUtils.routerGoto(router)
            RouterUtils.routerGoto("https://file.polaristest.net/static/app/index.html#/actCache/inviteList")
        }
        mBinding.rechargeList.setOnClickListener {
//            GlobalPopupManager.showRechargePopup(this, mViewModel, isNeedBottom = false)
        }
        mBinding.rechargeError.setOnSingleClickListener {
            XPopup.Builder(this)
                .isDestroyOnDismiss(true)
                .asCustom(
                    ChargeFailureDialog(this)
                ).show()
        }
//        mBinding.rechargeList.setOnSingleClickListener {
//            Router.instance.setPath(RoutePath.RECHARGE).navigation()
//            GlobalPopupManager.showRechargePopup(this, mViewModel)
//        }
        mBinding.systemMessage.setOnSingleClickListener {
            val id = mBinding.otherId.text.toString()
            if (!TextUtils.isEmpty(id)) {
                Router.instance.setPath(RoutePath.USER_SPACE_ACTIVITY)
                    .withInt("page", UserConst.From.UNKNOWN)
                    .withLong("userId", id.toLong()).navigation(this)
            } else {
                Router.instance.setPath(RoutePath.USER_SPACE_ACTIVITY)
                    .withInt("page", UserConst.From.UNKNOWN)
                    .withLong("userId", UserManager.getUser().userId!!).navigation(this)
            }

        }
        mBinding.liveVideo.setOnSingleClickListener {
            val id = mBinding.otherId.text.toString()

            if (!TextUtils.isEmpty(id)) {

                StartCallProxy.builder(this)
                    .startCall(
                        mViewModel,
                        UserConst.From.UNKNOWN,
                        UserConst.LastFrom.UNKNOWN,
                        LiveConst.SenderFrom.MANUAL,
                        id.toLong()
                    )
                return@setOnSingleClickListener
            }

            if (UserManager.getUser().userId?.toInt() == 3008) {
                StartCallProxy.builder(this)
                    .startCall(
                        mViewModel,
                        UserConst.From.UNKNOWN,
                        UserConst.LastFrom.UNKNOWN,
                        LiveConst.SenderFrom.MANUAL,
                        3010
                    )
            } else {
                StartCallProxy.builder(this)
                    .startCall(
                        mViewModel,
                        UserConst.From.UNKNOWN,
                        UserConst.LastFrom.UNKNOWN,
                        LiveConst.SenderFrom.MANUAL,
                        3008
                    )
            }
        }

        mBinding.tvBangDan.setOnSingleClickListener {
            startActivity(Intent(this, UserRankActivity::class.java))
        }
        mBinding.tvInvite.setOnSingleClickListener {
            Router.instance.setPath(RoutePath.USER_AGENCY_INVITE_ACTIVITY).navigation(this)
        }

        mBinding.startVideo.setOnSingleClickListener {
//            GlobalPopupManager.showCompletePopup()
//            GlobalPopupManager.showUserProfilePopup(3008)
        }
        mBinding.joinLive.setOnSingleClickListener {
            val id = mBinding.otherId.text.toString()
            if (!TextUtils.isEmpty(id)) {
                LiveUtils.toSpectator(id.toLong()).navigation(this)
                return@setOnSingleClickListener
            }

            if (UserManager.getUser().userId?.toInt() == 3008) {
                LiveUtils.toSpectator(3010).navigation(this)
            } else {
                LiveUtils.toSpectator(3008).navigation(this)
            }

        }

        mBinding.tvUpgrade.setOnSingleClickListener {
            GlobalDialogManager.showUpgradeDialog(
                this,
                AppVersionVo(
                    1,
                    "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest",
                    forceUpdate = false,
                    promptOnlyOnce = true
                )
            )
        }
        mBinding.openparty.setOnSingleClickListener {
            StartLiveProxy.builder(this)
                .startPartyLive(mViewModel, LiveConst.Type.PARTY_LIVE, null)
        }
        mBinding.opengameparty.setOnSingleClickListener {
            StartLiveProxy.builder(this)
                .startGamePartyLive(
                    mViewModel,
                    LiveConst.Type.PARTY_GAME_LIVE,
                    LiveConst.GameType.DOMINO,
                    0,
                    1000
                )
        }
        mBinding.toparty.setOnSingleClickListener {
            val id = mBinding.otherId.text.toString()
            Router.instance.setPath(RoutePath.PARTY_SPECTATOR_ACTIVITY)
                .withInt(LiveConst.Params.LIVETYPE, LiveConst.Type.PARTY_LIVE)
                .withLong(LiveConst.Params.ANCHORID, id.toLong()).navigation(this)
        }
        mBinding.joingameparty.setOnSingleClickListener {
            val id = mBinding.otherId.text.toString()
            Router.instance.setPath(RoutePath.PARTY_GAME_SPECTATOR_ACTIVITY)
                .withLong(LiveConst.Params.ANCHORID, id.toLong()).navigation(this)
        }
        mViewModel.resp.observe(this) { resultState ->
            parseState(resultState, {
                XPopup.Builder(this)
                    .isDestroyOnDismiss(true)
                    .asCustom(AwardProgressDialog(this, it))
                    .show()
            })
        }
        mBinding.backpack.setOnSingleClickListener {
            Router.instance.setPath(RoutePath.BACKPACK_ACTIVITY)
                .navigation(this)
        }
        mBinding.clearDay.setOnSingleClickListener {
            CommonCache.savePolymerizationTime(0)
            CacheUtil.saveFloatingViewTips(false)
            ToastGravity.showGravity("Time clear success")
        }
    }

}